package com.hzit.payweb.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.hzit.paycommon.paycommonapi.enums.StatusEnum;
import com.hzit.paycommon.paycommonapi.req.PayReq;
import com.hzit.payweb.mapper.PaySerialMapper;
import com.hzit.payweb.model.PaySerial;
import com.hzit.payweb.service.IPaySerialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class PaySerialServiceImpl implements IPaySerialService {

    @Autowired
    private PaySerialMapper paySerialMapper;

    /**
     * 生成待支付的流水
     * @param payReq
     * @return
     */
    @Override
    public PaySerial createPaySerial(PayReq payReq) {

        PaySerial paySerial = new PaySerial();
        paySerial.setAmount(new BigDecimal(payReq.getAmount()));
        Date date = new Date();
        paySerial.setCreateTime(date);
        paySerial.setUpdateTime(date);
        paySerial.setMchOrderNo(payReq.getMchOrderNo());
        paySerial.setOverTime(payReq.getOverTime());
        paySerial.setSubject(payReq.getSuject());
        paySerial.setStatus(String.valueOf(StatusEnum.UNPAY.getCode())); //待支付
        paySerial.setUserId(payReq.getUserId());
        paySerial.setBody(paySerial.getBody());

        //生成流水号
        Snowflake snowflake = new Snowflake(1,1);
        long serial = snowflake.nextId();
      paySerial.setPaySerialNo(String.valueOf(serial));
        paySerialMapper.insertSelective(paySerial);
        return paySerial;

    }


    /**
     * 更新流水状态
     * @param
     * @return
     */
    @Override
    public boolean updatePaySerialStatus(String serialNo,int status) {
        return true;
    }

    @Override
    public PaySerial queryInfoByOutTradeNo(String outTradeNo) {
        PaySerial paySerial = new PaySerial();
        paySerial.setAmount(new BigDecimal(100));
        paySerial.setPaySerialNo("123");

        return paySerial;
    }

    @Override
    public boolean updatePaySerialStatus(PaySerial paySerial) {
        int i = paySerialMapper.updateByPrimaryKeySelective(paySerial);
        if(i>0){
            return true;
        }
        return false;
    }


    public static void main(String[] args) {
        Snowflake snowflake = new Snowflake(1,1);

        long serial = snowflake.nextId();

        System.out.println(serial);

    }

}
